package com.app.service.Impl;

import com.app.mapper.StudyMapper;
import com.app.mapper.StudyRecordMapper;
import com.app.model.*;
import com.app.service.StudyService;
import com.app.util.CommonHelp;
import com.app.vo.StudyVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudyServiceImpl implements StudyService {
    @Autowired
    private StudyMapper studyMapper;
    @Autowired
    private StudyRecordMapper studyRecordMapper;

    @Override
    public List<SystemModel> selectTypeList() {
        return studyMapper.selectTypeList();
    }

    @Override
    public StudyVo selectStudyList(Integer type, String title, Integer page, Integer limit, Integer villageId) {
        if (page == null || limit == null) {
            return null;
        }
        page = (page - 1) * limit;
        if (title != null && !title.equals("")) {

            List<StudyModel> studyModels = studyMapper.selectStudyByTitle(title, page, limit, villageId);
//            for (int i = 0; i < studyModels.size(); i++) {
//                Integer num = selectNum(studyModels.get(i).getId());
//                studyModels.get(i).setNum(num);
//            }
            QueryWrapper<StudyModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("title", title);
            if (villageId != null && villageId != 0) {
                queryWrapper.eq("village_id", villageId);
            }
            Integer count = studyMapper.selectCount(queryWrapper);
            StudyVo vo = new StudyVo();
            vo.setList(studyModels);
            vo.setCount(count);
            return vo;
        }
        if (type != null && type == 0) {
            List<StudyModel> studyModels = studyMapper.selectStudyList(page, limit, villageId);
//            for (int i = 0; i < studyModels.size(); i++) {
//                Integer num = selectNum(studyModels.get(i).getId());
//                studyModels.get(i).setNum(num);
//            }
            Integer count;
            if (villageId != null && villageId != 0) {
                QueryWrapper<StudyModel> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("village_id", villageId);
                count = studyMapper.selectCount(queryWrapper);
            } else {
                count = studyMapper.selectCount(null);
            }
            StudyVo vo = new StudyVo();
            vo.setList(studyModels);
            vo.setCount(count);
            return vo;
        } else {
            List<StudyModel> studyModels = studyMapper.selectStudyListByType(type, page, limit, villageId);
//            for (int i = 0; i < studyModels.size(); i++) {
//                Integer num = selectNum(studyModels.get(i).getId());
//                studyModels.get(i).setNum(num);
//            }
            QueryWrapper<StudyModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", type);
            if (villageId != null && villageId != 0) {
                queryWrapper.eq("village_id", villageId);
            }
            Integer count = studyMapper.selectCount(queryWrapper);
            StudyVo vo = new StudyVo();
            vo.setList(studyModels);
            vo.setCount(count);
            return vo;
        }
    }

    @Override
    public StudyModel selectContentById(Integer id) {
        QueryWrapper<StudyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        StudyModel model = studyMapper.selectOne(queryWrapper);
        model.setNum(model.getNum() + 1);
        studyMapper.updateById(model);
        return model;
    }

    @Override
    public StudyModel selectContentById(Integer id, Integer userId) {
        QueryWrapper<StudyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        StudyModel studyModel = studyMapper.selectOne(queryWrapper);
        studyModel.setUserId(userId);
        studyModel.setNum(studyModel.getNum() + 1);
        studyMapper.updateById(studyModel);
        return studyModel;
    }

    @Override
    public void insertStudyRecord(StudyRecordModel model) {
        if (model.getUserId() == null || model.getUserId() == 0) {
            return;
        }
        model.setCreateTime(CommonHelp.getNowTimestamp());
        studyRecordMapper.insert(model);
    }

    private Integer selectNum(Integer studyId) {
        return studyRecordMapper.selectNum(studyId);
    }
}
